package cn.dly.login.service.impl;

import cn.dly.login.entity.PointsDetails;
import cn.dly.login.entity.User;
import cn.dly.login.exception.LRException;
import cn.dly.login.mapper.PointsDetailsMapper;
import cn.dly.login.mapper.UserMapper;
import cn.dly.login.service.UserService;
import cn.dly.login.utils.Container;
import cn.dly.login.utils.JwtUtils;
import cn.dly.login.utils.MD5;
import cn.dly.login.vo.InfoVo;
import cn.dly.login.vo.LoginVo;
import cn.dly.login.vo.RegisterVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hrd
 * @since 2021-07-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Resource
    PointsDetailsMapper pointsDetailsMapper;

    @Override
    public String login(LoginVo loginVo) {
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        System.out.println(username+" "+password);
        if (StringUtils.isEmpty(username)
                || StringUtils.isEmpty(password)) {
            throw new LRException(20001, "error");
        }
        User user = userMapper.selectOne(
                new QueryWrapper<User>().eq("username", username));
        if (user == null) {
            throw new LRException(20001, "error");
        }
        if (!MD5.encrypt(password).equals(user.getPassword())) {
            throw new LRException(20001, "error");
        }
        String token = JwtUtils.getJwtToken(user.getUsername(), user.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String username=registerVo.getUsername();
        String nickname=registerVo.getNickname();
        String code=registerVo.getCode();
        String password=registerVo.getPassword();
        String mobile=registerVo.getMobile();
        if(StringUtils.isEmpty(username)
                ||StringUtils.isEmpty(nickname)
                ||StringUtils.isEmpty(code)
                ||StringUtils.isEmpty(password)){
            throw new LRException(20001,"error");
        }
        String mobleCode=redisTemplate.opsForValue().get(mobile);
        if(!code.equals(mobleCode)){
            throw new LRException(20001,"error");
        }
        Integer count=userMapper.selectCount(new QueryWrapper<User>().eq("mobile",mobile));
        if(count.intValue()>0){
            throw  new LRException(20001,"error");
        }
        count=userMapper.selectCount(new QueryWrapper<User>().eq("username",username));
        if(count>0){
            throw new LRException(20001,"error");
        }
        User user=new User()
                .setPassword(MD5.encrypt(password))
                .setNickname(nickname)
                .setUsername(username)
                .setMobile(mobile)
                .setUrl("http://wanderingdream.oss-cn-chengdu.aliyuncs.com/.jpeg/20210715/a9ecb53c865845c7aa90e9ceb1ce7e34.jpeg");
        this.save(user);
    }

    @Override
    public LoginVo getLoginVo(String username) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        LoginVo loginVo=new LoginVo();
        BeanUtils.copyProperties(user,loginVo);
        return loginVo;
    }

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
    }

    @Override
    public Integer getyzu(String username) {
        Integer count=userMapper.selectCount(new QueryWrapper<User>().eq("username",username));
        return count;
    }

    @Override
    public Integer getyzm(String mobile) {
        Integer count=userMapper.selectCount(new QueryWrapper<User>().eq("mobile",mobile));
        return count;
    }

    @Override
    public Integer modify(String password,String username) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        user.setPassword(MD5.encrypt(password));
        int count=userMapper.update(user,new QueryWrapper<User>().eq("username",username));
        return count;
    }

    @Override
    public InfoVo getInfoVo(String username) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        InfoVo infoVo=new InfoVo();
        BeanUtils.copyProperties(user,infoVo);
        return infoVo;
    }

    @Override
    public Integer modify(InfoVo infoVo,String username) {
        User user=new User();
        BeanUtils.copyProperties(infoVo,user);
        System.out.println(username+"*****************");
        user.setUsername(username);
        int count=userMapper.update(user,new QueryWrapper<User>().eq("username",username));
        return count;
    }

    @Override
    public Integer upload(String url,String username) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        user.setUrl(url);
        int count=userMapper.update(user,new QueryWrapper<User>().eq("username",username));
        return count;
    }

    @Override
    public List<PointsDetails> details(String username) {
        List<PointsDetails> list=pointsDetailsMapper.selectList(new QueryWrapper<PointsDetails>()
        .eq("username",username));
        for(int i=0;i<list.size();i++){
            SimpleDateFormat sdf=sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            list.get(i).setTime(sdf.format(list.get(i).getDate()));
        }
        return list;
    }

    @Override
    public Boolean yzmm(String username, String password) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        if(user.getPassword().equals(MD5.encrypt(password))){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public Integer modp(String username, String password) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        user.setPassword(MD5.encrypt(password));
        int count=userMapper.update(user,new QueryWrapper<User>().eq("username",username));
        return count;
    }

    @Override
    public String geturl(String username) {
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        return user.getUrl();
    }

    @Override
    public List<InfoVo> getall() {
        List<User> us=userMapper.selectList(null);
        List<InfoVo> is=new ArrayList<InfoVo>();
        for(int i=0;i<us.size();i++)
        {
            InfoVo infoVo=new InfoVo();
            BeanUtils.copyProperties(us.get(i),infoVo);
            is.add(infoVo);
        }
        return is;
    }

    @Override
    public Integer modall(InfoVo infoVo) {
        String username=infoVo.getUsername();
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
        User user1=new User();
        BeanUtils.copyProperties(infoVo,user1);
        user1.setPassword(user.getPassword());
        int count=userMapper.update(user1,new QueryWrapper<User>().eq("username",username));
        return count;
    }
}
